//
//  BaseViewButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/30.
//

import Cocoa


class BaseViewButton: NSView {

    private var mouseInButton = false
    /**
     当allowsState为true时，state状态值为4个
        分别为：off：表示正常日期，on：普通节日、纪念日，holiday：放假时间，work：倒休加班时间
     */
    var allowsState:Bool = false
    var target:Any?
    private var _event: NSEvent = NSEvent.init()
    var onClick: ((_ sender: BaseViewButton, _ event: NSEvent)->())?
    
    var isEnable: Bool = true {
        didSet {
            enableIsHander(sender: self, enable: isEnable)
        }
    }
    
    var state: StateValue = .off {
        didSet {
            stateChange()
        }
    }
    
    init() {
        super.init(frame: NSRect.zero)
        initialize()
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize() {
        
        addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
    }
    
    override var wantsUpdateLayer: Bool {
        return true
    }
    
    override func updateLayer() {
        if !isEnable {
            return
        }
        if mouseInButton {
            mouseInEnterd(sender: self, event: _event)
        } else {
            mouseInExited(sender: self, event: _event)
        }
    }
    
    
    
    /**
     如需实现鼠标移入事件，请实现mouseInEnterd方法
     */
    @available(*, unavailable)
    override func mouseEntered(with event: NSEvent) {
        self.needsDisplay = true
        self.mouseInButton = true
        _event = event
    }
    
    /**
     如需实现鼠标移出事件，请实现mouseInExited方法
     */
    @available(*, unavailable)
    override func mouseExited(with event: NSEvent) {
        self.needsDisplay = true
        self.mouseInButton = false
        _event = event
    }
    
    /**
     如需实现鼠标点击事件，请实现mouseInDown方法
     */
    @available(*, unavailable)
    override func mouseDown(with event: NSEvent) {
        if !isEnable {
            return
        }
        self.needsDisplay = true
        if allowsState {
            if state == .work {
                state = .off
            } else {
                state = StateValue.init(rawValue: state.rawValue + 1)!
            }
        } else {
            if state == .off {
                state = .on
            } else {
                state = .off
            }
        }
        if onClick != nil {
            onClick?(self, event)
        } else {
            mouseInDown(sender: self, event: event)
        }
    }
    
    func mouseInDown(sender: BaseViewButton, event: NSEvent) {
    }
    
    func mouseInEnterd(sender: BaseViewButton, event: NSEvent) {
    }
    
    func mouseInExited(sender: BaseViewButton, event: NSEvent) {
    }
    
    func enableIsHander(sender: BaseViewButton, enable: Bool) {
    }
    
    func stateChange () {
        
    }
    
    enum StateValue:Int, CaseIterable {
        case off = 0
        case on = 1
        case holiday = 2
        case work = 3
    }
}
